<?php
    /**
    * return in Json format
    * @author tunv
    * @since Sep 20, 2012
    * 
    * Change logs
    * Oct 2, 2012 - tunv - Make this action compatible with product edit page
    */
    if (!defined('EXPONENT')) exit('');
    if (exponent_permissions_check(array('administrate','configure'),$loc)){
        //// pagination
        $black_list = '';
        $search_email = '';
        $sql_where = '1';
        if(isset($_REQUEST['black_list']))
        {
            $black_list = $_REQUEST['black_list'];
            if($black_list=='Yes')
            {
             $sql_where .= " and blacklist='1'";   
            }
            else
            {
               $sql_where .= " and blacklist='0'";    
            }
        }
        if(isset($_REQUEST['search_email']))
        {
            $search_email = $_REQUEST['search_email'];   
            $sql_where .= " and email LIKE '%{$search_email}%'"; 
        }
        //sort id
        $sortOrder = "DESC";
        $sort = "id";
        $sort = " ORDER BY {$sort} {$sortOrder}";
        $page=1;
        if (isset($_REQUEST['page']))
            $page=intval($_REQUEST['page']);
        $pageSize=10;
        if (isset($_REQUEST['pageSize']))
            $pageSize=intval($_REQUEST['pageSize']);
        $start=($page-1)*$pageSize;
        $data=$db->selectObjects('maillinglist',"{$sql_where} {$sort} limit {$start},{$pageSize}");
        foreach ($data  as $k=>$v)
        {
            if ($v->user_id > 0)
                $v->user_type = 1; // normal
            else
                $v->user_type = 0; // Maillinglist user type
            
        }
        
        $ret_object = null;
        $ret_object->total = $db->countObjects('maillinglist',"{$sql_where} {$sort} limit {$start},{$pageSize}");
        $ret_object->rows = $data;
        echo json_encode($ret_object);
    }
    die();
?>